﻿using System;
using System.Web.Mvc;
using Codeplus.Library.Models;
using Codeplus.Library.Web;
using Codeplus.Library.Controllers;
using Codeplus.Library.Utils;

namespace Codeplus.Controllers
{
    public class HomeController : Controller
    {
        private const string Localhost = "127.0.0.1";

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AboutUs()
        {
            return View();
        }

        public ActionResult ContactUs()
        {
            return View();
        }

        public ActionResult Expertise()
        {
            return View();
        }

        public ActionResult Showcase()
        {
            return View();
        }

        public ActionResult Support()
        {
            return View();
        }

        public ActionResult Workflow()
        {
            return View();
        }

        public ActionResult SoftwareTools()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        public ActionResult CollectContactInfo(ContactUsModel model)
        {
            try
            {
                var ipaddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                var helper = new Contactus();
                var isValid = true;
                GeolocationModel geolocation = null;
                if (ipaddress != Localhost)
                {
                    geolocation = (new Geolocation()).GetLocation(ipaddress);
                    isValid = helper.IsCaptchaSuccessful(ipaddress, model);
                }
                if (isValid)
                {
                    try
                    {
                        helper.CollectContactInfo(model, ipaddress, geolocation);
                        (new Mail()).Send(Mail.SenderAccount,
                                          Mail.Opportunity,
                                          string.Format("New Opportunity : [{0}]", model.FirstName),
                                          model.ToString(),
                                          true);
                        return this.Json(new
                        {
                            Success = true,
                            Message = "Your information has been saved"
                        });
                    }
                    catch (ApplicationException ex)
                    {
                        (new Mail()).Send(Mail.SenderAccount,
                                          Mail.Support,
                                          "Application Error",
                                          string.Format("{0}<br/>{1}<br/>{2}", model, ex,
                                                        Webrequest.ParseServerVariables(Request.ServerVariables)),
                                          true);
                        return this.Json(new
                        {
                            Success = false,
                            Message = "Snap! Something has broken. Please try again later"
                        });
                    }

                }
                else
                {
                    return this.Json(new
                    {
                        Success = false,
                        Message = "Please enter the text as it appears in the image correctly"
                    });
                }    
            }
            catch(Exception ex)
            {
                (new Mail()).Send(Mail.SenderAccount,
                                  Mail.Support,
                                  "Application Error",
                                  string.Format("{0}<br/>{1}<br/>{2}", model, ex,
                                                Webrequest.ParseServerVariables(Request.ServerVariables)),
                                  true);
                return this.Json(new
                {
                    Success = false,
                    Message = "Some unexpected error has occured. Please try later."
                });
            }
        }
    }
}
